package com.yn.xr.front.news.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yn.xr.common.constants.CommonDef.SucceedOrFaultType;
import com.yn.xr.common.constants.NewsInfoDef.NewsType;
import com.yn.xr.common.page.Page;
import com.yn.xr.common.util.JSONUtils;
import com.yn.xr.front.base.action.BaseAction;
import com.yn.xr.front.base.constants.SystemConstants;
import com.yn.xr.front.news.service.NewsCommentService;
import com.yn.xr.news.mode.NewsComment;
import com.yn.xr.news.mode.NewsInfo;

@Scope("prototype")
@Controller("newsCommentAction")
public class NewsCommentAction extends BaseAction {

	private static final long serialVersionUID = -2565536293838174567L;
	// 日志对象
	private static final Logger logger = LoggerFactory.getLogger(NewsCommentAction.class);

	@Resource(name = "newsCommentService")
	private NewsCommentService newsCommentService; // 新闻评论

	// 新闻图片服务器地址
	private String cdn_url = SystemConstants.CDN_URL;

	// 新闻唯一标识ID
	private Integer newsId;

	// 用户惠信ID
	private Long userId;

	// 内容
	private String words;

	
	private NewsInfo newsInfo;
	
	private Page<NewsComment> page;
	
	private Map<String,Object> datas = new HashMap<String,Object>();
	
	/**
	 * 
	 * @function: 添加新闻评论
	 * @return String
	 * @exception
	 * @author: 黄伟
	 * @since 1.0.0
	 */
	public String addComment() {
		Map<String, Object> refData = new HashMap<String, Object>();
		try {
			// 用户未登录则不能进行评论
			if (super.session.get(SystemConstants.USER_SESSION_UID) != null) {
				Long uid = (Long) super.session.get(SystemConstants.USER_SESSION_UID);
				String accName = (String) super.session.get(SystemConstants.USERACCNAME_SESSION_INFO);
				words = filter(words.trim());
				refData = newsCommentService.addCommentOrRevert("1", uid,newsId, words, accName, super.request.getRemoteAddr());
			} else {
				refData.put("loginSign", "false");
			}
			String json = JSONUtils.toJSONString(refData);
			super.outStr(json);
		} catch (Exception e) {
			logger.error("【添加新闻评论出错:】" + e);
		}
		return null;
	}

	/**
	 * 过滤非法字符，html标签
	 * 
	 * @param text
	 * @return
	 * @see [类、类#方法、类#成员]
	 * @param
	 * @author lifang
	 * @version 1.0.0
	 * @data 2015年7月17日
	 */
	private String filter(String text) {
		text = text.replace("/\'/g", "");
		text = text.replace("/\\/g", "");
		text = text.replace("/<[^>]*>/g", "");
		return text;
	}

	
	
	public String getCommentList() {
		try {
			if (null != newsInfo && newsInfo.getNewsId() != null) {
				String hql = "  status = ? and newsId = ? ";
				Object[] queryParams = new Object[]{ SucceedOrFaultType.succeed.getValue(),newsInfo.getNewsId() };
				page = newsCommentService.queryCommentPage( hql , queryParams , pageNo, pageSize);
				datas.put("commentPage", page);
			}
		} catch (Exception e) {
			logger.error("NewsCenterAction's getCommentList【获取评论分页失败, 失败原因】：" + e);
		}
		return "commentPage";
	}
	
	public String getCdn_url() {
		return cdn_url;
	}

	public void setCdn_url(String cdn_url) {
		this.cdn_url = cdn_url;
	}

	public Integer getNewsId() {
		return newsId;
	}

	public void setNewsId(Integer newsId) {
		this.newsId = newsId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getWords() {
		return words;
	}

	public void setWords(String words) {
		this.words = words;
	}

	public NewsInfo getNewsInfo() {
		return newsInfo;
	}

	public void setNewsInfo(NewsInfo newsInfo) {
		this.newsInfo = newsInfo;
	}

	public Page<NewsComment> getPage() {
		return page;
	}

	public void setPage(Page<NewsComment> page) {
		this.page = page;
	}

	public Map<String, Object> getDatas() {
		return datas;
	}

	public void setDatas(Map<String, Object> datas) {
		this.datas = datas;
	}
}
